<template>
  <div class="xtx-goods-page" v-if="goods">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="'/category/' + goods.categories[1].id">
          {{ goods.categories[1].name }}
        </XtxBreadItem>
        <XtxBreadItem :to="'/category/sub/' + goods.categories[0].id">
          {{ goods.categories[0].name }}
        </XtxBreadItem>
        <XtxBreadItem>{{ goods.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 商品信息左边 -->
        <div class="goods-info-left">
          <goods-image :images="goods.mainPictures" />
          <GoodsSales />
        </div>
        <!-- 商品信息右边 -->
        <div class="goods-info-right">
          <GoodsName :goods="goods" />
          <!-- 规格组件 -->
          <GoodsSku :goods="goods" @change="changeSku" />
          <XtxNumbox label="数量" v-model="count" :max="goods.inventory" />
          <XtxButton
            type="primary"
            @click="insertCart"
            style="margin-top: 20px;"
          >
            加入购物车
          </XtxButton>
        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant :goodsId="goods.id" />
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <GoodsTabs />
          <!-- 注意事项 -->
          <GoodsWarn />
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :goodsId="goods.id" :type="1" />
          <goods-hot :goodsId="goods.id" :type="2" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import GoodsRelevant from './components/goods-relevant.vue'
import { nextTick, provide, ref, watch } from 'vue'
import { findGoods } from '@/api/goods'
import { useRoute } from 'vue-router'
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodsName from './components/goods-name.vue'
import GoodsSku from './components/goods-sku.vue'
import GoodsTabs from './components/goods-tabs.vue'
import GoodsHot from './components/goods-hot.vue'
import GoodsWarn from './components/goods-warn.vue'
import Message from '@/components/library/Message'
import { useStore } from 'vuex'
export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsRelevant,
    GoodsImage,
    GoodsSales,
    GoodsName,
    GoodsSku,
    GoodsTabs,
    GoodsHot,
    GoodsWarn,
  },
  setup() {
    const goods = ref(null)
    const count = ref(1)
    const route = useRoute()
    // 注意单页面应用路由变化，需要重新获取商品详情
    watch(
      () => route.params.id,
      async (id) => {
        // 如果不是商品详情页不处理
        if (route.path !== `/product/${id}`) return
        const { result } = await findGoods(id)
        goods.value = result
        count.value = 1
      },
      { immediate: true },
    )

    provide('goods', goods)

    // sku改变时候触发
    const changeSku = (sku) => {
      if (sku.skuId) {
        goods.value.price = sku.price
        goods.value.oldPrice = sku.oldPrice
        goods.value.inventory = sku.inventory
      }
      currSku.value = sku
    }

    const currSku = ref(null)
    const store = useStore()

    // 加入购物车
    const insertCart = () => {
      if (currSku.value && currSku.value.skuId) {
        const { skuId, inventory: stock, attrsText } = currSku.value
        const { name, price, id, mainPictures } = goods.value
        store
          .dispatch('cart/insertCart', {
            skuId,
            stock,
            attrsText,
            name,
            nowPrice: price,
            price,
            id,
            picture: mainPictures[0],
            selected: true,
            isEffective: true,
            count: count.value,
          })
          .then(() => {
            count.value = 1
            Message({ type: 'success', msg: '加入购物车成功！' })
          })
      } else {
        Message({ type: 'warn', msg: '请选择商品规格' })
      }
    }

    return { goods, changeSku, count, insertCart }
  },
}
</script>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .goods-info-left {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .goods-info-right {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
